package com.xiaobai.demo;

import com.xiaobai.util.ThreadUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 线程打断
 */
@Slf4j
public class ThreadRunningInterrupted {
    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
           while (true){
                //这里就需要通过打断标记 来判断是否需要退出循环
               if(Thread.currentThread().isInterrupted()){
                   log.debug("Thread is interrupted");
                   break;
               }
           }
        });
        t1.start();
        ThreadUtil.sleep(1);
        log.debug("Thread interrupt");
        t1.interrupt();
        log.debug("Thread status is {}",t1.getState());
        log.debug("Thread interrupt status is {}",t1.isInterrupted());
    }
}
